package org.rabbitmq.rabbitextensions.controller;


import jakarta.annotation.Resource;
import org.rabbitmq.rabbitextensions.config.RabbitMQConfig;
import org.rabbitmq.rabbitextensions.constant.Constants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/produce")
public class ProduceController {
    @Resource(name = "rabbitTemplate")
//    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Resource(name="confirmRabbitTemplate")
    private RabbitTemplate confirmRabbitTemplate;

    //事务
    @Resource(name = "transRabbitTemplate")
    private RabbitTemplate transRabbitTemplate;

    @RequestMapping("/ack")
    public String ack() {
        rabbitTemplate.convertAndSend(Constants.ACK_EXCHANGE, "ack", "consumer ack mode test...");
        return "OK";
    }
    //非持久化消息
    @RequestMapping("/pres")
    public String pres(){
        Message message = new Message("Presistent test...".getBytes(), new MessageProperties());
        //消息非持久化
//        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        //消息持久化
        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        System.out.println(message);
        rabbitTemplate.convertAndSend(Constants.PRES_EXCHANGE, "pres", message);
        return "OK";
    }

    @RequestMapping("/confirm")
    public String confirm(){
        CorrelationData correlationData = new CorrelationData("1");
        confirmRabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE,"confirm","confirm test...",correlationData);
        return "OK";
    }
    //消息被退回时,发送信息
    @RequestMapping("/returns")
    public String returns(){
        CorrelationData correlationData = new CorrelationData("2");
        confirmRabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE,"confirm111","confirm test...",correlationData);
        return "OK";
    }
    //重试机制
    @RequestMapping("/retry")
    public String retry(){
        rabbitTemplate.convertAndSend(Constants.RETRY_EXCHANGE, "retry", "retry test...");
        return "OK";
    }
    //过期时间ttl:
    //设置消息的ttl:
    @RequestMapping("/ttl")
    public String ttl(){
        String ttl = "10000";//10s 单位微妙
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE,"ttl","ttl test ..",messagePostProcessor -> {
            messagePostProcessor.getMessageProperties().setExpiration(ttl);
            return messagePostProcessor;
        });

        String ttl2 = "20000";//10s 单位微妙
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE,"ttl","ttl test ..",messagePostProcessor -> {
            messagePostProcessor.getMessageProperties().setExpiration(ttl2);
            return messagePostProcessor;
        });
        return "OK";
    }
    //测试队列的过期时间:30秒
    @RequestMapping("/ttl2")
    public String ttl2(){
            rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE,"ttl","ttl test ..");
        return "OK";
    }
    //死信
    @RequestMapping("/dl")
    public String dl(){
//        for(int i=0;i<20;i++){
//            rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,"norm","dl test...");
//        }
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,"norm","dl test...");
        System.out.printf("%tc [dl]消息发送\n",new Date());
        return "OK";
    }

    //借助ttl+死信实现延迟队列
    @RequestMapping("/delay")
    public String  delay(){
        System.out.println("delay...");
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE, "normal", "ttl test 10s...", message -> {
            message.getMessageProperties().setExpiration("10000");  //单位: 毫秒, 过期时间为10s
            return message;
        });

        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE, "normal", "ttl test 30s...", message -> {
            message.getMessageProperties().setExpiration("30000");  //单位: 毫秒, 过期时间为30s
            return message;
        });

        System.out.printf("%tc [delay]消息发送成功 \n", new Date());
        return "OK";
    }
    //延迟队列
    @RequestMapping("/delay2")
    public String delay2(){
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE, "delay", "delay test 30s...", message -> {
            message.getMessageProperties().setDelayLong(30000L);  //单位: 毫秒, 过期时间为30s
            return message;
        });
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE, "delay", "delay test 10s...", message -> {
            message.getMessageProperties().setDelayLong(10000L);  //单位: 毫秒, 延迟时间为10s
            return message;
        });
        System.out.printf(" %tc [delay]消息发送成功",new Date());
        return "OK";

    }
//    //事务
//    @RequestMapping("/trans")
//    public String trans(){
//        System.out.println("[trans]消息发送");
//        transRabbitTemplate.convertAndSend("",Constants.TRANS_QUEUE,"trans test 1");
////        int n = 10/0;
//        transRabbitTemplate.convertAndSend("",Constants.TRANS_QUEUE,"trans test 2");
////        rabbitTemplate.convertAndSend("",Constants.TRANS_QUEUE,"trans test1");
//////        int n = 10/0;
////        rabbitTemplate.convertAndSend("",Constants.TRANS_QUEUE,"trans test2");
//        return "OK";
//
//    }

    @RequestMapping("/qos")
    public String qos() {
        System.out.println("qos test...");
        //发送普通消息
        for (int i = 0; i < 20; i++) {
            rabbitTemplate.convertAndSend(Constants.QOS_EXCHANGE, "qos", "qos test..."+i);
        }
        return "消息发送成功";
    }
}
